<?php
/**
 * Created by PhpStorm.
 * User: qufan
 * Date: 2016/12/27 0027
 * Time: 23:55
 */

namespace Home\Controller;


class FeedbackController extends HomeController
{
    public function index(){
        $this->meta_title = '反馈管理';
        $this->display();
    }
    /**
    * add : 添加统计连表
    * user: 边个懂
    * time: 20170522
    **/
    public function feedbackList($limit, $offset, $search = null){
        $fields = 'of.opinion_feedback_id, u.real_name, of.content, of.create_time, of.state';
        if(!empty($search)){
            $map['u.real_name'] = array('like', "%{$search}%");
        }
        if(!empty($this->uids)){
            $map['_string'] = "u.user_id in(".implode(',', $this->uids).")";
        }
        $record = M('OpinionFeedback')->alias('of')->join('user u on of.user_id=u.user_id')->field($fields)->where($map)->limit($offset, $limit)->select();
        $total_count = M('OpinionFeedback')->alias('of')->join('user u on of.user_id=u.user_id')->where($map)->count();
        // $total_count = M('OpinionFeedback')->alias('of')->where($map)->count();
        empty($total_count) && $total_count = 0;
        $result['rows'] = $record;
        $result['total'] = $total_count;
        $this->ajaxReturn($result);
    }

    public function delete(){
        $ids = $_POST['selectedIds'];
        if(!empty($ids) && count($ids) > 0){
            if(!empty($this->uids)){
                $map['_string'] = "user_id in(".implode(',', $this->uids).")";
            }
            $map['opinion_feedback_id'] = array('in', $ids);
            $res = M('OpinionFeedback')->where($map)->delete();
            if($res !== false){
                logOperation('反馈管理','删除反馈');
                $this->sucessJson();
            }
        }
        $this->errorJson('删除失败');

    }

    public function setRead(){
        $ids = $_POST['selectedIds'];
        if(!empty($ids) && count($ids) > 0){
            if(!empty($this->uids)){
                $map['_string'] = "user_id in(".implode(',', $this->uids).")";
            }
            $map['opinion_feedback_id'] = array('in', $ids);
            $res = M('OpinionFeedback')->where($map)->setField('state', 1);
            if($res !== false){
                logOperation('反馈管理','设置已读');
                $this->sucessJson();
            }
        }
        $this->errorJson('更新失败');
    }

}